﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MainFrameDesign.Component;
using System.Xml;

namespace MainFrmTool.ToolBox
{
    public partial class MyTool : UserControl
    {
        public delegate void OnDragDelegate(ToolBoxItem ai);
        public event OnDragDelegate OnDrag;
        public Dictionary<string, ToolBoxGroup> Groups = new Dictionary<string, ToolBoxGroup>();
        public MyTool()
        {
            
            InitializeComponent();
            DisableScroll();
        }

        private void DisableScroll()
        {
            MyScroll.Enabled = false;
            MyScroll.Maximum = 0;
            MyScroll.Minimum = 0;
            MyScroll.Value = 0;
            MyScroll.Visible = false;
        }
        public void CallAutoScroll()
        {
            int height = 0;
            foreach (var item in Groups.Values)
            {
                height += item.GetHeight();
            }
            if (height > MyDraw.Height)
            {
                MyScroll.Enabled = true;
                MyScroll.Maximum = (height-MyDraw.Height)+MyScroll.LargeChange+10;
                MyScroll.Minimum = 0;
                MyScroll.Visible = true;
            }
            else 
            {
                DisableScroll();
            }
        }

        private void MyScroll_ValueChanged(object sender, EventArgs e)
        {
            InvalidateDraw();
        }

        private void MyDraw_Paint(object sender, PaintEventArgs e)
        {
            int y = 0;
            Graphics g = e.Graphics;
            g.Clear(Color.Gainsboro);
            g.TranslateTransform(0, -MyScroll.Value);
            foreach (var item in Groups.Values)
            {
                item.Draw(g, ref y,MyDraw.Width);
            }
        }

        private void MyDraw_Resize(object sender, EventArgs e)
        {
            CallAutoScroll();
        }

        public void InvalidateDraw() 
        {
            MyDraw.Invalidate();
        }
        private void MyDraw_MouseLeave(object sender, EventArgs e)
        {
            if (Hover != null) 
            {
                Hover.IsHover = false;
            }
            MyDraw.Invalidate();
        }

        ToolBoxItem Hover = null;
        private void MyDraw_MouseMove(object sender, MouseEventArgs e)
        {
            Point point = Control.MousePosition;
            point = MyDraw.PointToClient(point);
            point.Y += MyScroll.Value;
            ToolBoxItem HoverTem = null;
            foreach (var item in Groups.Values)
            {
                HoverTem = item.Contain(point);
                if (HoverTem != null)
                {
                    break;
                }
            }

            if (HoverTem != null)
            {
                if (Hover != null)
                {
                    Hover.IsHover = false;
                }
                Hover = HoverTem;
                Hover.IsHover = true;
                MyDraw.Invalidate();
            }
        }
        ToolBoxItem SelectItem = null;
        private void MyDraw_MouseDown(object sender, MouseEventArgs e)
        {
            if (Hover != null)
            {
                if (SelectItem != null)
                {
                    SelectItem.IsSelect = false;
                }
                if (Hover is ToolBoxGroup) 
                {
                    ToolBoxGroup SelectGroup = (ToolBoxGroup)Hover;
                    if (SelectGroup == SelectItem)
                    {
                        if (SelectGroup.IsOpen)
                        {
                            SelectGroup.IsOpen = false;
                        }
                        else 
                        {
                            SelectGroup.IsOpen = true;
                        }
                    }
                }
                SelectItem = Hover;
                SelectItem.IsSelect = true;
                bool b = SelectItem is ToolBoxGroup;
                if (b)
                {
                    CallAutoScroll();
                }
                MyDraw.Invalidate();
                if (!b&&OnDrag!=null)
                {
                    OnDrag(SelectItem);
                }
            }
        }

        public void LoadXml(System.Xml.XmlDocument doc)
        {
            XmlNode root = doc.DocumentElement;
            foreach (XmlNode item in root.ChildNodes)
            {
                MyComponent myc = new MyComponent();
                XmlDocument doctem = new XmlDocument();
                doctem.LoadXml(item.OuterXml);
                myc.LoadXml(doctem);
                string groupname = myc.CurrentComponent.GroupName;
                ToolBoxGroup group =null;
                if (!Groups.ContainsKey(groupname))
                {
                    group = new ToolBoxGroup();
                    group.Text = groupname;
                    group.IsOpen = true;
                    this.Groups.Add(groupname, group);
                }
                else 
                {
                   group=this.Groups[groupname];
                }
                ToolBoxItem tbi = new ToolBoxItem();
                tbi.Text = myc.CurrentComponent.ChinaName;
                tbi.TypeName = myc.CurrentComponent.TypeName;
                tbi.ItemControl = myc;
                group.AppendItem(tbi);
            }
        }
    }
}
